package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;

/**
 * 查看物流
 *
 * @author wu
 */
public class DeliveryExpressLookFragment extends Fragment {

  private WebView mwebView;
  private ProgressBar mProgressBar;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.logisticLook);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_logistic_look, container, false);
    mwebView = (WebView) view.findViewById(R.id.wv_webview);
    mProgressBar = (ProgressBar) view.findViewById(R.id.pb_progressBar);
    initData();
    // 加载需要显示的网页
    mwebView.loadUrl("http://www.kuaidi100.com/");
    return view;
  }

  private void initData() {
    //		// 打开网页时不调用系统浏览器
    //		mwebView.setWebViewClient(new WebViewClient() {
    //			@Override
    //			public boolean shouldOverrideUrlLoading(WebView widget, String url) {
    //				LogUtil.i("overUrl="+url);
    //				widget.loadUrl(url);
    //				return true;
    //			}
    //		});
    // 滚动条风格，为0指滚动条不占用空间，直接覆盖在网页上
    mwebView.setScrollBarStyle(0);
    WebSettings webSettings = mwebView.getSettings();
    webSettings.setSupportZoom(true); // 支持缩放
    // 设置WebView属性，能够执行Javascript脚本
    webSettings.setJavaScriptEnabled(true);
    // html页面大小自适应
    webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);

    mwebView.setWebChromeClient(new WebChromeClient() {
      @Override public void onProgressChanged(WebView view, int newProgress) {

        if (newProgress == 100) {
          mProgressBar.setVisibility(View.GONE);// 加载完网页进度条消失
        } else {
          mProgressBar.setVisibility(View.VISIBLE);// 开始加载网页时显示进度条
          mProgressBar.setProgress(newProgress);// 设置进度值
        }
      }
    });
  }

  /**
   * 处理WebView跳转返回
   */
  public void goBack() {
    mwebView.goBack();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      /**
       * 响应返回按钮
       */
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
